package com.myapp.tools.configbuilder;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sourceforge.pinyin4j.PinyinHelper;

import com.myapp.tools.bean.ToolsConfig;
import com.myapp.tools.configbuilder.base.FieldBean;
import com.myapp.tools.util.BuilderTools;

public class ConfigBuilder {

	private String sheetName = null;

	private String sheetNamePY = null;

	private String fileName = null;

	private List<FieldBean> fieldBeans = null;

	private String idColumn = null;

	private String toPinying(String name) {
		int length = sheetName.length();
		StringBuilder sb = new StringBuilder();
		char c;
		String[] py;
		for (int i = 0; i < length; i++) {
			c = sheetName.charAt(i);
			if (c >= 'a' && c <= 'z') {
				sb.append(c);
				continue;
			}

			if (c >= 'A' && c <= 'Z') {
				sb.append(c);
				continue;
			}

			if (c >= '0' && c <= '9') {
				sb.append(c);
				continue;
			}
			py = PinyinHelper.toHanyuPinyinStringArray(c);
			if (py.length > 0) {
				sb.append(py[0]);
			}
		}

		return sb.toString();
	}

	public void builderMsg(String fileName, String sheetName, Object[][] data) {
		this.fileName = fileName;
		this.sheetName = sheetName;
		this.sheetNamePY = this.toPinying(sheetName);
		// 组织数据

		// field英文名字
		String[] enName = (String[]) data[0];

		// field类型
		String[] fieldType = (String[]) data[1];

		// field中文名字
		String[] znName = (String[]) data[2];

		// 是否注释列
		String[] isComment = (String[]) data[3];

		// 是否需要国际化
		String[] needGlobal = (String[]) data[4];

		// 是否客户端加载，便于以后自动生成时，给前端加载
		String[] needClient = (String[]) data[5];

		// id列
		String[] idColumn = (String[]) data[6];

		int length = enName.length;
		fieldBeans = new ArrayList<FieldBean>();
		FieldBean tmp;
		for (int i = 0; i < length; i++) {
			tmp = new FieldBean();
			tmp.setEnName(enName[i]);
			tmp.setZnName(znName[i]);
			tmp.setFieldType(fieldType[i]);
			tmp.setNeedClient(needClient[i]);
			tmp.setNeedGlobal(needGlobal[i]);
			tmp.setIsComment(isComment[i]);
			tmp.setIdColumn(idColumn[i]);
			if (idColumn[i] != null && "1".equals(idColumn[i])) {
				this.idColumn = enName[i];
			}

			if ("0".equals(isComment[i])) {// 如果不是注释列，则添加
				fieldBeans.add(tmp);
			}
		}

		// 构建服务器配置文件
		this.builderServerConfig();
	}

	/**
	 * 生成服务器端配置信息
	 * 
	 * @param msgBean
	 *            消息bean
	 */
	private void builderServerConfig() {
		String templatePath = ToolsConfig.appToolsConfigBean.getBase_Path()
				+ File.separator
				+ ToolsConfig.appToolsConfigBean.getConfig_ServerTemplate();

		String savePath = ToolsConfig.appToolsConfigBean.getBase_Path()
				+ File.separator
				+ "export"
				+ File.separator
				+ "src"
				+ File.separator
				+ ToolsConfig.appToolsConfigBean.getConfig_ServerPackage()
						.replaceAll("\\.", "\\" + File.separator);

		String tmp = (String.valueOf(this.sheetNamePY.charAt(0))).toUpperCase();
		String fileName = tmp + this.sheetNamePY.substring(1) + "Bean.java";

		// 准备数据
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("ConfigPackage",
				ToolsConfig.appToolsConfigBean.getConfig_ServerPackage());
		root.put("FileName", this.fileName);
		root.put("IdColunm", this.idColumn);
		root.put("Name", this.sheetNamePY);
		root.put("SheetName", this.sheetName);
		root.put("FieldBeans", this.fieldBeans);

		// 生成文件
		BuilderTools.build(templatePath, savePath, fileName, root);
	}
}
